<?
function drizzo_site_name() {
	$site_name = drizzo_global_options("site_name");
	return $site_name;
}

function drizzo_site_description() {
	$site_description = drizzo_global_options("site_description");
	return $site_description;
}

function drizzo_recent_posts($limit = 5, $list_type = "ul", $list_item_before = "<li>", $list_item_after = "</li>") {
	if(substr($list_type, 0, 1) == "<") {
		$list_type = substr($list_type, 1);
	}
	if(substr($list_type, -1, 1) == ">") {
		$list_type = substr($list_type, 0, (strlen($list_type) - 1));
	}
	$the_posts = drizzo_get_the_posts($limit, 0, "publish,private", "blog,aside");
	echo "<" . $list_type . " id=\"recent_posts\">\n";
	foreach($the_posts as $the_post) {
		echo $list_item_before . "<a href=\"/blog/" . $the_post['permalink_name'] . "\" title=\"" . $the_post['title'] . "\">" . $the_post['title'] . "</a>" . $list_item_after . "\n";
	}
	echo "</" . $list_type . ">\n";
}

function drizzo_theme($option = null) {
	global $drizzo_options;
	
	drizzo_db_connect();
	
	$theme_lookup = mysql_query("SELECT `value` FROM `$drizzo_options` WHERE `name`='theme'");
	if($theme_lookup) {
		$theme_name = mysql_fetch_array($theme_lookup);
		$theme_name = $theme_name[0];
	} else {
		$theme_name = "default";
	}
	
	if(!$option) {
		return $theme_name;
	}
	
	if(($option == "stylesheet")|| ($option == "css")) {
		$stylesheet = "http://" . domain_name . "/drizzo-themes/" . $theme_name . "/style.css";
		return $stylesheet;
	}
	
	if($option == "directory") {
		$directory = "http://" . domain_name . "/drizzo-themes/" . $theme_name . "/";
		return $directory;
	}

	if($option == "local_dir") {
		$directory = $_SERVER['DOCUMENT_ROOT'] . "/drizzo-themes/" . $theme_name . "/";
		return $directory;
	}
}

function drizzo_startup() {
	if(drizzo_config_file_check()) {
		drizzo_update_version_number();
		drizzo_login();
		drizzo_user_info();
		drizzo_get_page_id();
		drizzo_get_page_type();
	}
}

function drizzo_parse_the_uri() {
	if(!$_GET) {
		$doc_root = $_SERVER['DOCUMENT_ROOT'];
		$req_uri = $_SERVER['REQUEST_URI'];
		$scpt = $_SERVER['SCRIPT_FILENAME'];

		$url = strip_tags($req_uri);
		$url_array = explode("/",$url);
		array_shift($url_array); //the first one is empty anyway

		if(!empty($url_array)) {
			$final = count($url_array) - 1;
			if(empty($url_array[$final])) {
				$final = count($url_array) - 2;
			} 

			if($url_array[0] == ("blog" || "page" || "netcasts" || "asides" || "events")) {
				$drizzo_page_type = $url_array[0];
				$drizzo_post_id = $url_array[$final];
			} elseif($url_array[0] == "archive") {
				$drizzo_page_type = "archive";
				$drizzo_offset = $url_array[1] * 10;
			} else {
				$drizzo_post_id = $url_array[0];
			}

			$drizzo_uri_vars = array(
				"page_type" => $drizzo_page_type,
				"post_id" => $drizzo_post_id,
				"offset" => $drizzo_offset
				);
			return $drizzo_uri_vars;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function drizzo_feed_digg($username, $limit) {
	$digg = new lastRSS;
	$digg->cache_dir = './cache';
	$digg->cache_time = 3600;
	$digg->items_limit = $limit;
	$digg_url = "http://www.digg.com/rss/" . $username . "/index2.xml";

	$digg_string = "<ul id=\"digg_rss\">\n";
	if ($rs = $digg->get($digg_url)) {
		foreach($rs['items'] as $item) {
			$digg_string .= "<li class=\"digg_link\"><a href=\"" . $item['link'] . "\" title=\"" . $item['title'] . "\">" . $item['title'] . "</a><br />
			<span class=\"rss_desc\">" . $item['description'] . "</span></li>\n";
		}
	} else {
		$digg_string .= "<li>" . $digg_url . " is not available.</li>";
	}		
	$digg_string .= "</ul>\n";
	return $digg_string;
}

function drizzo_feed_lastfm($username, $limit) {
	$lastfm = new lastRSS;
	$lastfm->cache_dir = './cache';
	$lastfm->cache_time = 3600;
	$lastfm->items_limit = $limit;
	$lastfm_url = "http://ws.audioscrobbler.com/1.0/user/" . $username . "/recenttracks.rss";
	$time_offset = -5;
	function is_neg ($num) {
	   return $num < 0;
	}

	function turn_neg ($num) {
	   if (is_neg ($num)) {
	       return $num - $num * 2;
	   } else {
	       return abs ($num);
	   }
	}
	$offset = turn_neg($time_offset);
	if($offset != $time_offset) { $offset_math = "subtract"; }
	if($offset == $time_offset) { $offset_math = "add"; }

	$lastfm_string = "<ul id=\"lastfm_rss\">\n";
	
	if ($rs = $lastfm->get($lastfm_url)) {
		foreach($rs['items'] as $item) {
			$artist_track = preg_split('/ – /', $item['title']);
			$item['artist'] = $artist_track[0];
			$item['track'] = $artist_track[1];
			$timestamp = preg_split('/ /', $item['pubDate']);
			$item['day'] = $timestamp[0];
			if($item['day'] == "Sun,") { $item['day'] = "Sunday"; }
			if($item['day'] == "Mon,") { $item['day'] = "Monday"; }
			if($item['day'] == "Tue,") { $item['day'] = "Tuesday"; }
			if($item['day'] == "Wed,") { $item['day'] = "Wednesday"; }
			if($item['day'] == "Thur,") { $item['day'] = "Thursday"; }
			if($item['day'] == "Fri,") { $item['day'] = "Friday"; }
			if($item['day'] == "Sat,") { $item['day'] = "Saturday"; }
			$item['day_no'] = $timestamp[1];
			$item['month'] = $timestamp[2];
			if($item['month'] == "Jan") { $item['month'] = "January"; }
			if($item['month'] == "Feb") { $item['month'] = "February"; }
			if($item['month'] == "Mar") { $item['month'] = "March"; }
			if($item['month'] == "Apr") { $item['month'] = "April"; }
			if($item['month'] == "May") { $item['month'] = "May"; }
			if($item['month'] == "Jun") { $item['month'] = "June"; }
			if($item['month'] == "Jul") { $item['month'] = "July"; }
			if($item['month'] == "Aug") { $item['month'] = "August"; }
			if($item['month'] == "Sep") { $item['month'] = "September"; }
			if($item['month'] == "Oct") { $item['month'] = "October"; }
			if($item['month'] == "Nov") { $item['month'] = "November"; }
			if($item['month'] == "Dec") { $item['month'] = "December"; }
			$item['year'] = $timestamp[3];
			$item['date'] = $item['day'] . ", " . $item['month'] . " " . $item['day_no'];
			$item['offset'] = $timestamp[5];
			$time = preg_split('/:/', $timestamp[4]);
			$hour = $time[0];
			if($offset_math == "add") { $hour = $hour + ($item_offset + $offset); }
			if($offset_math == "subtract") { $hour = $hour - ($item_offset + $offset); }
			if($hour < 12) { $meridian == "AM"; }
			if($hour == 12) { $meridian == "Noon"; }
			if(($hour == "0") || ($hour == "00")) { $hour = 12; $meridan = "Midnight"; }
			if($hour > 12) { $hour = $hour - 12; $meridian = "PM"; }
			$minute = $time[1];
			$item['time'] = $hour . ":" . $minute . " " . $meridian;
			$lastfm_string .= "<li class=\"lastfm_link\"><a href=\"" . $item['link'] . "\" title=\"" . $item['artist'] . " - " . $item['track'] . "\">" . $item['artist'] . " - " . $item['track'] . "</a><br />
			<span class=\"rss_desc\">Played on " . $item['date'] . " at " . $item['time'] . "</span></li>\n";
		}
	} else {
		$lastfm_string .= "<li>" . $lastfm_url . " is not available.</li>";
	}	
	$lastfm_string .= "</ul>\n";
	return $lastfm_string;
}

function drizzo_last_post_date() {
	global $drizzo_posts;
	
	drizzo_db_connect();
	
	$now = time();
	
	$lookup = @mysql_query("SELECT `posted_date` FROM `$drizzo_posts` WHERE `posted_date`<'$now' ORDER BY `posted_date` LIMIT 1");
	if($lookup) {
		$last_date = mysql_fetch_array($lookup);
		$last_date = $last_date['posted_date'];
		return $last_date;
	} else {
		return false;
	}
}
?>